package cn.schoolwow.ams.module.block.form.initial.flow;

import cn.schoolwow.ams.domain.block.form.FormBlockConfigAware;
import cn.schoolwow.ams.domain.block.form.FormBlockOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CheckFormBlockOptionFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        Map<String,FormBlockConfigAware> map = applicationContext.getBeansOfType(FormBlockConfigAware.class);

        if(map.isEmpty()){
            flowContext.brokenCurrentCompositeBusiness("无FormBlock配置类列表信息");
        }
        List<FormBlockOption> formBlockOptionList = new ArrayList<>();
        for(FormBlockConfigAware blockConfigAware : map.values()){
            FormBlockOption formBlockOption = blockConfigAware.getAMSBlockConfig();
            formBlockOption.routerBlock = blockConfigAware;

            String className = blockConfigAware.getClass().getName();
            CheckInstance.newCheck()
                    .notNullCheck(formBlockOption, "clazz表配置,涉及类名:["+className+"]")
                    .notEmptyCheck(formBlockOption.projectName, "项目名称,涉及类名:["+className+"]")
                    .notEmptyCheck(formBlockOption.displayName, "表显示名称,涉及类名:["+className+"]")
                    .notNullCheck(formBlockOption.formClass, "表单实体类,涉及类名:["+className+"]");
            formBlockOptionList.add(formBlockOption);
        }
        flowContext.putTemporaryData("formBlockOptionList", formBlockOptionList);
    }

    @Override
    public String name() {
        return "检查FormBlockOption对象";
    }

}
